/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package l1j.server.server.model.Instance;

import l1j.server.server.ActionCodes;
import l1j.server.server.GeneralThreadPool;
import l1j.server.server.IdFactory;
import l1j.server.server.datatables.SprTable;
import l1j.server.server.model.L1World;
import l1j.server.server.serverpackets.S_AttackMissPacket;
import l1j.server.server.serverpackets.S_Dexup;
import l1j.server.server.serverpackets.S_DoActionGFX;
import l1j.server.server.serverpackets.S_DollPack;
import l1j.server.server.serverpackets.S_HPUpdate;
import l1j.server.server.serverpackets.S_MPUpdate;
import l1j.server.server.serverpackets.S_NPCTalkReturn;
import l1j.server.server.serverpackets.S_OwnCharStatus;
import l1j.server.server.serverpackets.S_ServerMessage;
import l1j.server.server.serverpackets.S_SkillIconGFX;
import l1j.server.server.serverpackets.S_SkillSound;
import l1j.server.server.serverpackets.S_Strup;
import l1j.server.server.templates.L1MagicDoll;
import l1j.server.server.templates.L1Npc;
import l1j.server.server.utils.Random;

public class L1DollInstance extends L1NpcInstance {
	private static final long serialVersionUID = 1L;

	public static final int DOLL_TIME = 1800000;
	private int _itemId;
	private int _itemObjId;
	private int run;
	private boolean _isDelete = false;

	// ターゲットがいない场合の处理
	@Override
	public boolean noTarget() {
		if ((_master != null) && !_master.isDead()
				&& (_master.getMapId() == getMapId())) {
			if (getLocation().getTileLineDistance(_master.getLocation()) > 2) {
				int dir = moveDirection(_master.getX(), _master.getY());
				setDirectionMove(dir);
				setSleepTime(calcSleepTime(getPassispeed(), MOVE_SPEED));
			} else {
				// 魔法娃娃 - 特殊动作
				dollAction();
			}
		} else {
			_isDelete = true;
			deleteDoll();
			return true;
		}

		// 祭司娃娃 add
		if (_master != null
				&& (_master.getCurrentHp() > 0)
				&& (_master.getCurrentHp() <= ((_master.getMaxHp() * _master.getHierarch()) / 10))
				&& (getCurrentMp() > 15)
				&& (getLocation().getTileLineDistance(_master.getLocation()) < 7)
				&& (_master.glanceCheck(_master.getX(), _master.getY()) == true)
				&& (getHierarch() == 1)
				&& (getNpcTemplate().get_npcId()== 99022 )) { // 银光祭司主动补血
			if (_master instanceof L1PcInstance) {
				L1PcInstance player = (L1PcInstance) _master;
				broadcastPacket(new S_SkillSound(player.getId(), 830));
				broadcastPacket(new S_AttackMissPacket(this, player.getId(), 19));
				player.setCurrentHp(player.getCurrentHp() + (80 + Random.nextInt(75)));
			//	player.sendPackets(new S_ServerMessage(77)); // 你觉得舒服多了。
				player.sendPackets(new S_HPUpdate(player.getCurrentHp(), player.getMaxHp()));
				setCurrentMp(getCurrentMp() - 20);
				if (player.isInParty()) {
					player.getParty().updateMiniHP(player);
				}
			}
		} else if (_master != null
				&& (_master.getCurrentHp() > 0)
				&& (_master.getCurrentHp() <= ((_master.getMaxHp() * _master.getHierarch()) / 10))
				&& (getCurrentMp() > 19)
				&& (getLocation().getTileLineDistance(_master.getLocation()) < 7)
				&& (_master.glanceCheck(_master.getX(), _master.getY()) == true)
				&& (getHierarch() == 1)
				&& (getNpcTemplate().get_npcId() == 99023)) { // 黄金祭司主动补血
			if (_master instanceof L1PcInstance) {
				L1PcInstance player = (L1PcInstance) _master;
				broadcastPacket(new S_SkillSound(player.getId(), 830));
				broadcastPacket(new S_AttackMissPacket(this, player.getId(), 19));
				player.setCurrentHp(player.getCurrentHp() + (105 + Random.nextInt(75)));
			//	player.sendPackets(new S_ServerMessage(77)); // 你觉得舒服多了。
				player.sendPackets(new S_HPUpdate(player.getCurrentHp(), player.getMaxHp()));
				setCurrentMp(getCurrentMp() - 20);
				if (player.isInParty()) {
					player.getParty().updateMiniHP(player);
				}
			}
		} else if (_master != null
				&& (_master.getCurrentHp() > 0)
				&& (_master.getCurrentHp() <= ((_master.getMaxHp() * _master.getHierarch()) / 10))
				&& (getCurrentMp() > 19)
				&& (getLocation().getTileLineDistance(_master.getLocation()) < 7)
				&& (_master.glanceCheck(_master.getX(), _master.getY()) == true)
				&& (getHierarch() == 1)
				&& (getNpcTemplate().get_npcId() == 99024)) { // 白金祭司主动补血 
			if (_master instanceof L1PcInstance) {
				L1PcInstance player = (L1PcInstance) _master;
				broadcastPacket(new S_SkillSound(player.getId(), 830));
				broadcastPacket(new S_AttackMissPacket(this, player.getId(), 19));
				player.setCurrentHp(player.getCurrentHp() + (125 + Random.nextInt(75)));
			//	player.sendPackets(new S_ServerMessage(77)); // \f1你觉得舒服多了。
				player.sendPackets(new S_HPUpdate(player.getCurrentHp(), player.getMaxHp()));
				setCurrentMp(getCurrentMp() - 20);
				if (player.isInParty()) {
					player.getParty().updateMiniHP(player);
				}
			}
		} else if (_master != null
				&& ((!_master.hasSkillEffect(68)
				&& getNpcTemplate().get_npcId() != 99022
				&& getNpcTemplate().get_npcId() != 99023)
				|| (!_master.hasSkillEffect(79)
						&& getNpcTemplate().get_npcId() != 99022)
				|| !_master.hasSkillEffect(48)
				|| !_master.hasSkillEffect(26)
				|| !_master.hasSkillEffect(42))
				&& (_master.getCurrentHp() > 0)
				&& (getCurrentMp() > 9)
				&& (getHierarch() == 1)) { // 加辅助效果
			L1PcInstance player = (L1PcInstance) _master;

			// 通畅气脉术
			if (!player.hasSkillEffect(26) && (getNpcTemplate().get_npcId() == 99022
					|| getNpcTemplate().get_npcId() == 99023
					|| getNpcTemplate().get_npcId() == 99024)) {
				player.addDex((byte) 5);
				player.sendPackets(new S_Dexup(player, 5, 1200));
				player.setSkillEffect(26, 1200 * 1000);
				player.broadcastPacket(new S_SkillSound(player.getId(), 750));
				player.sendPackets(new S_SkillSound(player.getId(), 750));
				player.sendPackets(new S_ServerMessage(294)); // 你变得敏捷。
				setCurrentMp(getCurrentMp() - 50);
			}
			// 体魄强健术
			if (!player.hasSkillEffect(42) && (getNpcTemplate().get_npcId() == 99022
					|| getNpcTemplate().get_npcId() == 99023
					|| getNpcTemplate().get_npcId() == 99024)) {
				player.addStr((byte) 5);
				player.sendPackets(new S_Strup(player, 5, 1200));
				player.setSkillEffect(42, 1200 * 1000);
				player.broadcastPacket(new S_SkillSound(player.getId(), 751));
				player.sendPackets(new S_SkillSound(player.getId(), 751));
				player.sendPackets(new S_ServerMessage(292)); // 你变得强壮。
				setCurrentMp(getCurrentMp() - 50);
			}
			// 灵魂升华
			if (!player.hasSkillEffect(79) && (getNpcTemplate().get_npcId() == 99023
					|| getNpcTemplate().get_npcId() == 99024)) {
				player.setAdvenHp(player.getBaseMaxHp() / 5);
				player.setAdvenMp(player.getBaseMaxMp() / 5);
				player.addMaxHp(player.getAdvenHp());
				player.addMaxMp(player.getAdvenMp());
				player.sendPackets(new S_HPUpdate(player.getCurrentHp(), player.getMaxHp()));
				player.setSkillEffect(79, 1200 * 1000);
				player.sendPackets(new S_SkillSound(player.getId(), 3935));
				player.broadcastPacket(new S_SkillSound(player.getId(), 3935));
				setCurrentMp(getCurrentMp() - 20);
				if (player.isInParty()) { // 
					player.getParty().updateMiniHP(player);
				}
				player.sendPackets(new S_MPUpdate(player.getCurrentMp(), player.getMaxMp()));
			}
			// 圣结界
			if (!player.hasSkillEffect(68) && getNpcTemplate().get_npcId() == 99024) {
				int dmg = 0;
				dmg /=2;
				player.setSkillEffect(68, 32 * 1000);
				player.broadcastPacket(new S_SkillSound(player.getId(), 228));
				player.sendPackets(new S_SkillSound(player.getId(), 228));
				player.sendPackets(new S_ServerMessage(314)); // 你感觉到有神秘的力量在保护你。
				setCurrentMp(getCurrentMp() - 30);
			}
			// 祝福魔法武器
			if (!player.hasSkillEffect(48) && (getNpcTemplate().get_npcId() == 99022
					|| getNpcTemplate().get_npcId() == 99023
					|| getNpcTemplate().get_npcId() == 99024)) {
				player.addDmgup(2);
				player.addHitup(2);
				player.setSkillEffect(48, 1200 * 1000);
				player.broadcastPacket(new S_SkillSound(player.getId(), 2176));
				player.sendPackets(new S_SkillSound(player.getId(), 2176));
				setCurrentMp(getCurrentMp() - 20);
			}
			broadcastPacket(new S_AttackMissPacket(this, player.getId(), 19));
		}
		// 祭司娃娃 end

		return false;
	}

	// 时间计测用
	class DollTimer implements Runnable {
		@Override
		public void run() {
			if (_destroyed) { // 既に破弃されていないかチェック
				return;
			}
			deleteDoll();
		}
	}

	public L1DollInstance(L1Npc template, L1PcInstance master, int itemId,
			int itemObjId) {
		super(template);
		setId(IdFactory.getInstance().nextId());

		setItemId(itemId);
		setItemObjId(itemObjId);
		GeneralThreadPool.getInstance().schedule(new DollTimer(), DOLL_TIME);

		setMaster(master);
		setX(master.getX() + Random.nextInt(5) - 2);
		setY(master.getY() + Random.nextInt(5) - 2);
		setMap(master.getMapId());
		setHeading(5);
		setLightSize(template.getLightSize());
		setMoveSpeed(1);
		setBraveSpeed(1);

		L1World.getInstance().storeObject(this);
		L1World.getInstance().addVisibleObject(this);
		for (L1PcInstance pc : L1World.getInstance().getRecognizePlayer(this)) {
			onPerceive(pc);
		}
		master.addDoll(this);
		if (!isAiRunning()) {
			startAI();
		}
		if (L1MagicDoll.isHpRegeneration(_master)) {
			master.startHpRegenerationByDoll();
		}
		if (L1MagicDoll.isMpRegeneration(_master)) {
			master.startMpRegenerationByDoll();
		}
		if (L1MagicDoll.isItemMake(_master)) {
			master.startItemMakeByDoll();
		}
	}

	public void deleteDoll() {
		broadcastPacket(new S_SkillSound(getId(), 5936));
		if (_master != null && _isDelete) {
			L1PcInstance pc = (L1PcInstance) _master;
			pc.sendPackets(new S_SkillIconGFX(56, 0));
			pc.sendPackets(new S_OwnCharStatus(pc));
		}
		if (L1MagicDoll.isHpRegeneration(_master)) {
			((L1PcInstance) _master).stopHpRegenerationByDoll();
		}
		if (L1MagicDoll.isMpRegeneration(_master)) {
			((L1PcInstance) _master).stopMpRegenerationByDoll();
		}
		if (L1MagicDoll.isItemMake(_master)) {
			((L1PcInstance) _master).stopItemMakeByDoll();
		}
		_master.getDollList().remove(getId());
		deleteMe();
	}

	@Override
	public void onPerceive(L1PcInstance perceivedFrom) {
		// 判断旅馆内是否使用相同钥匙
		if (perceivedFrom.getMapId() >= 16384 && perceivedFrom.getMapId() <= 25088 // 旅馆内判断
				&& perceivedFrom.getInnKeyId() != _master.getInnKeyId()) {
			return;
		}
		perceivedFrom.addKnownObject(this);
		perceivedFrom.sendPackets(new S_DollPack(this));
	}

	@Override
	public void onItemUse() {
	}

	@Override
	public void onGetItem(L1ItemInstance item) {
	}

	public int getItemObjId() {
		return _itemObjId;
	}

	public void setItemObjId(int i) {
		_itemObjId = i;
	}

	// 祭司娃娃 add
	public void onTalkAction(L1PcInstance player) {
		if (isDead()) {
			return;
		}
		if (_master.equals(player) && (getNpcTemplate().get_npcId() == 99022
				|| getNpcTemplate().get_npcId() == 99023
				|| getNpcTemplate().get_npcId() == 99024)) {
			String[] htmldata = null;
			String msg0 = "";
			String msg1 = String.valueOf(player.getHierarch() * 10);
			if (getHierarch() == 1) {
				msg0 = "辅助";
			} else {
				msg0 = "跟随";
			}
			htmldata = new String[] {
					getName(),
					String.valueOf(getCurrentMp()),
					String.valueOf(getMaxMp()),
					msg0, msg1 };
			player.sendPackets(new S_NPCTalkReturn(getId(), "Hierarch", htmldata));
		}
	}
	// 祭司娃娃回魔相关
	public void onFinalAction(L1PcInstance player, String action) {
	}

	@Override
	public void setCurrentMp(int i) {
		int currentMp = i;
		if (currentMp >= getMaxMp()) {
			currentMp = getMaxMp();
		}
		setCurrentMpDirect(currentMp);
		if (getMaxMp() > getCurrentMp()) {
			startMpRegeneration();
		}
	}
	// 祭司娃娃 end

	public int getItemId() {
		return _itemId;
	}

	public void setItemId(int i) {
		_itemId = i;
	}

	// 表情动作
	private void dollAction() {
		run = Random.nextInt(100) + 1;
		if (run <= 10) {
			int actionCode = ActionCodes.ACTION_Aggress; // 67
			if (run <= 5) 
				actionCode = ActionCodes.ACTION_Think; // 66

			broadcastPacket(new S_DoActionGFX(getId(), actionCode));
			setSleepTime(calcSleepTime(SprTable.getInstance().getSprSpeed(getTempCharGfx(),
							actionCode), MOVE_SPEED)); //
		}
	}
}
